<%--
  Created by IntelliJ IDEA.
  User: LLS
  Date: 2019/12/23
  Time: 13:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>内部数据源新增编辑</title>
    <link rel="stylesheet" href="/lib/layui/layui/css/layui.css"  media="all">
    <script type="text/javascript" src="/lib/layui/layui/layui.js"></script>
    <script type="text/javascript" src="/lib/layui/layui/layui.all.js"></script>
    <script type="text/javascript" src="/js/jquery/jquery-1.9.1.js"></script>
</head>
<style>
    body {
        margin: 0px;
        padding: 0px;
        height: 100%;
        min-width: 100%;
    }
    body {
        margin: 0;
        font-family: Simsun, Arial, sans-serif;
        font-size: 13px;
        line-height: 20px;
        color: #333333;
        background-color: #ffffff;
    }
    body {
        display: block;
        margin: 8px;
    }
    div {
        display: block;
    }
    .span3 {
        width: 220px;
    }
    [class*="span"] {
        float: left;
        min-height: 1px;
        margin-left: 20px;
    }
    ul {
        display: block;
        list-style-type: disc;
        margin-block-start: 1em;
        margin-block-end: 1em;
        margin-inline-start: 0px;
        margin-inline-end: 0px;
        padding-inline-start: 40px;
    }
    .bs-docs-sidenav {
        width: 208px;
        margin: 30px 0 0;
        padding: 0;
        background-color: #fff;
        -webkit-border-radius: 6px;
        -moz-border-radius: 6px;
        border-radius: 6px;
        -webkit-box-shadow: 0 1px 4px rgba(0,0,0,.065);
        -moz-box-shadow: 0 1px 4px rgba(0,0,0,.065);
        box-shadow: 0 1px 4px rgba(0,0,0,.065);
    }
    .nav-list {
        padding-left: 15px;
        padding-right: 15px;
        margin-bottom: 0;
    }
    .nav {
        margin-left: 0;
        margin-bottom: 20px;
        list-style: none;
    }
    ul, ol {
        padding: 0;
        margin: 0 0 10px 25px;
    }
    li {
        line-height: 20px;
    }
    body, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, textarea, p, blockquote, th, td, hr, button, article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
        margin: 0;
        padding: 0;
    }
    user agent stylesheet
    li {
        display: list-item;
        text-align: -webkit-match-parent;
    }
    .bs-docs-sidenav > li:first-child > a {
        -webkit-border-radius: 6px 6px 0 0;
        -moz-border-radius: 6px 6px 0 0;
        border-radius: 6px 6px 0 0;
    }
    .bs-docs-sidenav > .active > a {
        position: relative;
        z-index: 2;
        padding: 9px 15px;
        border: 0;
        text-shadow: 0 1px 0 rgba(0,0,0,.15);
        -webkit-box-shadow: inset 1px 0 0 rgba(0,0,0,.1), inset -1px 0 0 rgba(0,0,0,.1);
        -moz-box-shadow: inset 1px 0 0 rgba(0,0,0,.1), inset -1px 0 0 rgba(0,0,0,.1);
        box-shadow: inset 1px 0 0 rgba(0,0,0,.1), inset -1px 0 0 rgba(0,0,0,.1);
    }
    .nav-list > .active > a, .nav-list > .active > a:hover, .nav-list > .active > a:focus {
        color: #ffffff;
        text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.2);
        background-color: #0088cc;
    }
    .bs-docs-sidenav > li > a {
        display: block;
        width: 180px \9;
        margin: 0 0 -1px;
        padding: 8px 14px;
        border: 1px solid #e5e5e5;
    }
    .nav-list > li > a {
        padding: 3px 15px;
    }
    .nav-list > li > a, .nav-list .nav-header {
        margin-left: -15px;
        margin-right: -15px;
        text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
    }
    .nav > li > a {
        display: block;
    }
    a:visited {
        color: #1687cb;
        text-decoration: none;
    }
    a:link {
        color: #1687cb;
        text-decoration: none;
    }
    a {
        color: #0088cc;
        text-decoration: none;
    }
    a {
        outline: none;
    }
    ins, a {
        text-decoration: none;
    }
    user agent stylesheet
    a:-webkit-any-link {
        color: -webkit-link;
        cursor: pointer;
        text-decoration: underline;
    }
    li {
        line-height: 20px;
    }
    user agent stylesheet
    li {
        text-align: -webkit-match-parent;
    }
    .nav {
        margin-left: 0;
        margin-bottom: 20px;
        list-style: none;
    }
    ul, ol {
        list-style: none;
    }

    user agent stylesheet
    ul {
        list-style-type: disc;
    }
    li {
        display: list-item;
        text-align: -webkit-match-parent;
    }
    .bs-docs-sidenav > li > a {
        display: block;
        width: 180px \9;
        margin: 0 0 -1px;
        padding: 8px 14px;
        border: 1px solid #e5e5e5;
    }
    .bs-docs-sidenav {
        width: 208px;
        margin: 30px 0 0;
        padding: 0;
        background-color: #fff;
        -webkit-border-radius: 6px;
        -moz-border-radius: 6px;
        border-radius: 6px;
        -webkit-box-shadow: 0 1px 4px rgba(0,0,0,.065);
        -moz-box-shadow: 0 1px 4px rgba(0,0,0,.065);
        box-shadow: 0 1px 4px rgba(0,0,0,.065);
    }

    hr {
        height: 3px;
    }

    .flow_top_type {
        margin: 0px;
        padding: 0px;
        margin-top: 26px;
        margin-right: 10px;
        border-bottom: 3px solid #3f9bca;
        color: #3f9bca;
        font-size: 14px;
        font-weight: bold;
        margin-bottom: 16px;
    }
    .f_field_explain_label {
        /* background: #F0F8FA; */
        width: 90%;
        margin: 0px;
        padding: 0px;
        float: left;
        margin-bottom: 5px;
        border: 0px;
    }
    .f_field_explain_title_green {
        color: green;
        margin: 0px;
        padding: 0px;
        font-size: 12px;
    }

</style>
<body data-spy="scroll" data-target=".bs-docs-sidebar" style="">
<div name="body">



    <!-- 左侧导航部分开始 -->
    <div class="span3" id="nav_left">
        <ul class="nav nav-list bs-docs-sidenav" id="list_nav">
            <li class="active" name="check_field" item="first">
                <a href="#check_field" data-toggle="" style="cursor:default">
                    <i class="icon-chevron-right" style="margin-top:8px;"></i>
                    <span class="icon20-change_way_hover ">选择数据源创建方式 </span>
                    <i class="layui-icon layui-icon-next" style="margin-left: 38px;"></i>
                </a>
            </li>
            <li class="" name="basic_attr">
                <a href="#basic_attr" data-toggle="" style="cursor:default">
                    <i class="icon-chevron-right" style="margin-top:8px;"></i>
                    <span class="icon20-new_datasrc">新建数据源名称</span>
                    <i class="layui-icon layui-icon-next" style="margin-left: 64px;"></i>
                </a>
            </li>
            <li class="" name="defi_field">
                <a href="#defi_field" data-toggle="" style="cursor:default">
                    <i class="icon-chevron-right" style="margin-top:8px;"></i>
                    <span class="icon20-new_datasrc">定义字段</span>
                    <i class="layui-icon layui-icon-next" style="margin-left: 103px;"></i>
                </a>
            </li>
            <li class="" name="import_file" item="last" id="right" style="display: none;">
                <a href="#import_file" data-toggle="" style="cursor:default" class="data_radius_a">
                    <i class="icon-chevron-right" style="margin-top:8px;"></i>
                    <span class="icon20-defi_field">从文件导入</span>
                    <i class="layui-icon layui-icon-next" style="margin-left: 90px;"></i>
                </a>
            </li>
        </ul>
    </div>

    <div style="display:inline-block; width: 70%; margin-left: 30px;">
        <form class="layui-form">
                <%--第一步骤--%>
            <div data-type="check_field" style="display: block;">
                <div class="flow_top_type clearfix" style="width: 97%;">
                    <div class="float_left"><span class="icon20-condition_4">选择数据源创建方式</span></div>
                </div>
                <%--<hr class="layui-bg-blue">--%>
                <div class="layui-form-item">
                    <label class="layui-form-label">选择创建方式</label>
                    <div class="layui-input-block" >
                        <input type="radio" name="sex" value="手工建立" title="手工建立" checked="" lay-filter="radiofilter">
                        <%--暂时删除--%>
                        <%--<input type="radio" name="sex" value="从文件导入" title="从文件导入" lay-filter="radiofilter">--%>
                    </div>
                </div>

                <div class="f_field_explain_label" style="margin-left: 16px;">
                    <span id="explain" class="f_field_explain_title_green">
                        数据源与数据库的功能类似,新建数据源相当于新建表，可以对表结构、字段值进行修改<br>
                    </span>
                </div>
            </div>

                <%--第二步骤--%>
            <div data-type="basic_attr" style="display: none;">
                <div class="flow_top_type clearfix" style="width: 97%;">
                    <div class="float_left"><span class="icon20-condition_4">新建数据源名称</span></div>
                </div>
                <div class="layui-form-item" >
                    <label  style="margin-left: 6%; display: block; padding: 9px 15px; width: 80px; font-weight: 400; line-height: 20px;">数据源名称<span style="color:red;">*</span></label>
                    <div class="layui-input-block" style="margin-left: 57px;">
                        <input  type="text" name="dName"  placeholder="请输入标题" autocomplete="off" class="layui-input" style="width: 20%;">
                    </div>
                </div>
                <div class="f_field_explain_label" style="margin-left: 8%;">
                    <span id="explain" class="f_field_explain_title_green">数据源名称只能是字母、数字或数字和字母的组合</span>
                </div>
                <div class="layui-form-item">
                    <label style="margin-left: 6%; display: block; padding: 9px 15px; width: 80px; font-weight: 400; line-height: 20px;">数据源描述<span style="color:red;">*</span></label>
                    <div class="layui-input-block" style="margin-left: 57px;">
                        <input type="text" name="description" placeholder="请输入标题" autocomplete="off" class="layui-input" style="width: 20%;">
                    </div>
                </div>
            </div>

                <%--第三步骤--%>
            <div data-type="defi_field" style="display: none;">
                <div class="flow_top_type clearfix" style="width: 97%;">
                    <div class="float_left"><span class="icon20-condition_4">定义字段</span></div>
                </div>

                <div style="display: flex; float:left;">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 60px;"><span style="color:red;">*</span>字段名称</label>
                        <div class="layui-input-block" style="margin-left: 90px;" >
                            <input type="text" name="FieldName" placeholder="请输入标题" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 60px;"><span style="color:red;">*</span>字段描述</label>
                        <div class="layui-input-block" style="margin-left: 90px;">
                            <input type="text" name="FieldDoc"  placeholder="请输入标题" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 43px;padding-left: 0px;padding-right: 5px;">类型 </label>
                        <div class="layui-input-block" style="margin-left: 49px;width: 76.3px;">
                            <select name="selectType" lay-verify="required" lay-filter="selectType">
                                <option value="0">文本</option>
                                <option value="1">数字</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block" style="margin-left: 20px;">
                            <input type="checkbox" name="isselect" title="查询">
                            <div id="only" style="display: none">
                                <input type="checkbox" name="isonly" title="是否是唯一">
                            </div>
                        </div>
                    </div>
                </div>


                <div class="f_field_explain_label" style="margin-left: 17px;">
                    <span  class="f_field_explain_title_green">
                        字段名称必须是字母、下划线或数字和字母组合<br>
                    </span>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block" style="margin-left: 17px;">
                        <button name="Field" class="layui-btn">添加字段</button>
                    </div>
                </div>



                <div class="flow_top_type clearfix" style="width: 97%;">
                    <div class="float_left"><span class="icon20-condition_4">字段列表</span></div>
                </div>

                <%--表格--%>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="margin-left: 0px;">
                        <table id="demo" lay-filter="datatest"></table>
                    </div>
                </div>
            </div>

            <%--提交按钮--%>
            <button style="display: none;" name="formDemo" class="layui-btn" lay-submit lay-filter="formDemo">提交</button>

        </form>

    </div>
</div>
</body>
<script type="text/javascript" >
    // 获取地址栏参数值
    function getQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
    var form,layer,layedit,laydate,table; //定义实例
    var tab; //表格实例
    var datas=[]; //字段数据
    var dId= getQueryString('dId');

    //判断是否存在
    function isselects(array,item) {
        for (var i=0; array.length>i ;i++){
            if (array[i]===item){
                return true;
            }
        }
        return false;
    }

    //编辑初始化
    if (dId!=null){
        //左侧显示隐藏
        $('#nav_left ul').find('li').eq(0).css("display","none");
        $('#nav_left ul').find('li').eq(1).addClass('active');
        $('#nav_left ul').find('li').eq(1).find('span').html('编辑数据源名称');
        $('input[name="dName"]').attr('disabled',"true");
        $('input[name="dName"]').addClass('layui-disabled');
        $('button[lay-event="edit"]').css("display","block");
        //右侧显示隐藏
        $('form').children().eq(0).css("display","none");
        $('form').children().eq(1).css("display","block");
        $.get('/TerpServerController/findByDataSrcId',{dId:dId},function(res){
            if (res.flag){
                var obj=res.object;
                $('input[name="dName"]').val(obj.dName); //名称
                $('input[name="description"]').val(obj.dDesc); //描述
                for (var i=1;obj.list.length>i;i++){
                    var obj2={
                        isnew:true, //判断是否是编辑接口的数据
                        fieldName:obj.list[i].data1, //字段名称
                        FieldDoc:obj.list[i].data3 //字段描述
                    }
                    if (obj.list[i].data2==='text'){
                        obj2.selectType='文本';//字段类型
                    }else if (obj.list[i].data2==='int') {
                        obj2.selectType = '数字';//字段类型
                    }

                    //判断是否是唯一
                    if (obj.list[i].data4==='UNI'){
                        obj2.isonly='是';
                    }else {
                        obj2.isonly='否';
                    }

                    //判断是否是查询字段
                    if (isselects(obj.dNmarking.split(','),obj.list[i].data1)){
                        obj2.isselect='是';
                    }else {
                        obj2.isselect='否';
                    }
                    datas.push(obj2);
                }
                //渲染表格
                table.reload("demo", {
                    data:datas
                });
            }
        });
    }

    //编辑字段
    function edit(data) {
        layer.open({
            type: 1,
            shade:0,
            maxmin:true,
            area:['400px', '300px'],
            title: ['编辑字段', 'font-size:18px;'],
            content: '<div class="layui-form" lay-filter="editform" style="margin: 20px;">' +
            '<div class="layui-form-item">\n' +
            '    <label class="layui-form-label">字段名称</label>\n' +
            '    <div class="layui-input-block" style="width: 172px;">\n' +
            '      <input type="text" name="fieldName2" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">\n' +
            '    </div>\n' +
            '  </div>' +
            '<div class="layui-form-item">\n' +
            '    <label class="layui-form-label">字段描述</label>\n' +
            '    <div class="layui-input-block" style="width: 172px;">\n' +
            '      <input type="text" name="FieldDoc2" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">\n' +
            '    </div>\n' +
            '  </div>' +
            '<div class="layui-form-item">\n' +
            '    <div class="layui-input-block">\n' +
            '      <input type="checkbox" name="like[write]" title="查询">\n' +
            '      <input type="checkbox" name="like[read]" title="唯一">\n' +
            '    </div>\n' +
            '  </div>' +
            '</div>',
            btn:['确定'],
            success: function(layero, index){
                $('input[name="fieldName2"]').val(data.fieldName); //名称
                $('input[name="FieldDoc2"]').val(data.FieldDoc); //描述
                //查询
                if (data.isselect==='是'){
                    $('input[name="like[write]"]').attr("checked",""); //全选
                }
                //唯一
                if(data.isonly==='是'){
                    $('input[name="like[read]"]').attr("checked",""); //全选
                }
                form.render('checkbox', 'editform'); //渲染表单
                if (data.selectType==='文本'){
                    console.log($('input[name="like[read]"]').next());
                    $('input[name="like[read]"]').next().attr("style","display:none");
                }
            },
            yes: function(index, layero){
                //按钮【按钮一】的回调 form.render(type, filter); checked
                var fieldName=$('input[name="fieldName2"]').val(); //名称
                var FieldDoc=$('input[name="FieldDoc2"]').val(); //描述
                var iswrite=$('input[name="like[write]"]:checked').length; //查询
                var isread=$('input[name="like[read]"]:checked').length; //唯一
                for (var i=0;datas.length>i;i++){
                    if (datas[i].fieldName===data.fieldName){
                        datas[i].fieldName=fieldName; //名称
                        datas[i].FieldDoc=FieldDoc; //描述
                        datas[i].old=data.fieldName; //旧名称
                        //查询
                        if (iswrite===1){
                            datas[i].isselect='是';
                        }else{
                            datas[i].isselect='否';
                        }
                        //唯一’
                        if(isread===1){
                            datas[i].isonly='是';
                        }else {
                            datas[i].isonly='否';
                        }
                        break;
                    }
                }
                table.reload("demo", {
                    data:datas
                });
                layer.close(index);
            }
        });
    }

    //正则校验只能输入数字和英文
    function valPwd(v){
        var r=/^[a-zA-Z0-9]+$/g;
        return r.test(v);
    }

    //添加按钮字段点击事件
    $('button[name="Field"]').click(function () {
       var fieldName=$('input[name="FieldName"]').val(); //字段名称
       var FieldDoc=$('input[name="FieldDoc"]').val();  //字段描述
       var selectType=$('select[name="selectType"]').val();  //字段类型
       var isselect=$("input:checkbox[name='isselect']:checked").val();  //查询
        var isonly=$("input:checkbox[name='isonly']:checked").val();  //唯一
            //判断为空
        if(fieldName===undefined || fieldName===''){
            layer.msg('字段名称不能为空', {icon: 6});
            return false;
        }else if(FieldDoc===undefined || FieldDoc===''){
            layer.msg('字段描述不能为空', {icon: 6});
            return false;

        }
        var obj=new Object();
        obj['fieldName']=fieldName;
        obj['FieldDoc']=FieldDoc;
        if (selectType==='0'){
            obj['selectType']='文本';
        }else if(selectType==='1') {
            obj['selectType']='数字';
        }
        if (isselect==='on'){
            obj['isselect']='是';
        }else{
            obj['isselect']='否';
        }
        if (isonly==='on'){
            obj['isonly']='是';
        }else {
            obj['isonly']='否';
        }

        datas.push(obj);
        table.reload("demo", {
            data:datas
        });
        //清空
        $('input[name="FieldName"]').val(''); //字段名称
        $('input[name="FieldDoc"]').val('');  //字段描述
        $('select[name="selectType"]').val('0');  //字段类型
        $('input[name="isonly"]').prop('checked', false);  //唯一
        $('input[name="isselect"]').prop('checked', false);//查询
        $('#only').css("display","none");
        form.render();
        return false;
    });

    //按钮点击事件
    function butclick() {
        $('button[name="formDemo"]').click();
    }

    //下一步样式修改
    function Nextstep() {
        $('form').children().each(function(index,item){
            var style=$(this).css("display");
            if (style==="block" && index<2){
                var liName=$('form').children().eq(index+1).attr('data-type');
                if (liName==='defi_field'){
                    var dName=$('input[name="dName"]').val();
                    if (dName===undefined || dName===''){
                        layer.msg('数据源名称不能为空');
                        return false;
                    }
                    if (!valPwd(dName)){
                        layer.msg('数据源名称只能为英文和数字');
                        return false;
                    }
                    var description=$('input[name="description"]').val();
                    if (description===undefined || description===''){
                        layer.msg('数据源描述不能为空');
                        return false;
                    }

                    //只有新增才会走接口判断
                    if (dId===null){
                        var isres=isTable(dName);
                        if (isres){
                            layer.msg('已存在数据源');
                            return false;
                        }
                    }

                }
                $('li[name="'+liName+'"]').addClass('active');
                var datatype=$(this).attr('data-type');
                $('li[name="'+datatype+'"]').removeClass('active');
                $(this).css("display","none");
                $('form').children().eq(index+1).css("display","block");
                return false;
            }
        });
    }

    function isTable(dName) {
        var is=false;
        $.ajax({
            type:"GET",
            url:'/TerpServerController/countTable',
            data:{tableName:dName},
            dataType:"json",
            async:false,
            success:function(res) {
                if (res.flag){
                   is=res.flag
                }
            }
        });
        return is;
    }
    //上一步
    function Previous() {
        $('form').children().each(function(index,item){
            var style=$(this).css("display");
            if (style==="block" && index!=0){
                var liName=$('form').children().eq(index-1).attr('data-type');
                //编辑停止跳转
                if(dId!=null && liName==='check_field'){
                    return false;
                }
                $('li[name="'+liName+'"]').addClass('active');
                var datatype=$(this).attr('data-type');
                $('li[name="'+datatype+'"]').removeClass('active');
                $(this).css("display","none");
                $('form').children().eq(index-1).css("display","block");
                return false;
            }
        });
    }

    //判断所到步骤
    function isstep() {
        var is=false;
        $('#list_nav li').each(function(index,item){
            var name=$(this).attr('name');
            var clas=$(this).attr('class');
            if (clas==='active' && name==='defi_field'){
                is=true;
            }
        });
        return is;
    }
    layui.use(['form', 'layedit', 'laydate'], function() {
         form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , table = layui.table;
        form.render();

        //渲染表格
        //第一个实例
        tab=table.render({
            elem: '#demo'
            ,page: false //开启分页
            ,width:662
            ,cols: [[ //表头
                {field: 'fieldName', title: '字段名称',width:112,align: 'center'}
                ,{field: 'FieldDoc', title: '字段描述',width:112,align: 'center'}
                ,{field: 'selectType', title: '类型',width:112,align: 'center'}
                ,{field: 'isselect', title: '查询',width:112,align: 'center'}
                ,{field: 'isonly', title: '唯一',width:92,align: 'center'}
                ,{title: '操作',align: 'center',width:116,templet:function(d){
                    //编辑里的新增字段操作不显示编辑按钮
                    var str='<button type="button" lay-event="del" class="layui-btn layui-btn-xs layui-btn-radius layui-btn-danger">删除</button>';
                        if (dId!=null && d.isnew!=undefined && d.isnew){
                            str = '<button type="button" lay-event="edit"  class="layui-btn layui-btn-xs layui-btn-radius">编辑</button>'+str;
                        }
                        return str;
                    }}
            ]]
        });

        //单选框监听事件
        form.on('radio(radiofilter)', function(data){
          /*  console.log(data.elem); //得到radio原始DOM对象
            console.log(data.value); //被点击的radio的value值*/
            if (data.value==='手工建立'){
                $('li[name="import_file"]').prop("style","display:none"); //从文件导入
                $('li[name="defi_field"]').prop("style","display:block"); //定义字段
            }else if(data.value==='从文件导入'){
                $('li[name="import_file"]').prop("style","display:block");
                $('li[name="defi_field"]').prop("style","display:none");
            }
        });

        //监听下拉
        form.on('select(selectType)', function(data){
           /* console.log(data.elem); //得到select原始DOM对象
            console.log(data.value); //得到被选中的值
            console.log(data.othis); //得到美化后的DOM对象*/
            if (data.value==='1'){
                $('input[name="isonly"]').parent().css("display","block");
            }else {
                $('input[name="isonly"]').parent().css("display","none");
                $('input[name="isonly"]').prop('checked', false);
                $('input[name="isselect"]').prop('checked', false);
                form.render('checkbox');
            }
        });


        //监听工具条
        table.on('tool(datatest)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

           if(layEvent === 'del'){ //删除
               var index=tr.attr('data-index');
               //编辑删除是走接口删除字段
               if (dId!=null){
                   if (datas.length===1){
                       layer.msg('最少保留一个字段', {icon: 6});
                       return false;
                   }
                   if (data.isnew!=undefined && data.isnew){
                       var dName=$('input[name="dName"]').val(); //表名称
                       $.get('/TerpServerController/delField',{tableName:dName,fieldName:data.fieldName},function (res) {
                           if(res.flag){
                               layer.msg('删除成功', {icon: 6});
                           }
                       });
                   }
               }
               datas.splice(parseInt(index),1);
               table.reload("demo", {
                   data:datas
               });
            }else if (layEvent === 'edit'){ //编辑
               edit(data);
           }
        });

        //监听提交
        form.on('submit(formDemo)', function(data){
           /* console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}*/

           if (!isstep()){
               layer.msg('请按步骤操作');
               return false;
           }
           if (datas.length===0){
               layer.msg('最少添加一行字段列表');
               return false;
           }
            var field=data.field;
            var tables=[];
            var dNmarking=''; //要查询的字段
            var dNmarkingdesc=''; //要查询的字段描述
            for (var i=0;datas.length>i;i++){
                var obj={
                    data1:datas[i].fieldName,
                    data3:datas[i].FieldDoc
                }
                if (datas[i].selectType==='文本'){
                    obj.data2='text';
                }else if(datas[i].selectType==='数字'){
                    obj.data2='int(11)';
                }
                if (datas[i].isnew!=undefined){
                    obj.isnewedit=true; //修改
                    if (datas[i].old!=undefined){
                        obj.old=datas[i].old; //旧字段
                    }
                }else {
                    obj.isnewedit=false; //新增
                }
                //判断是否是查询字段
                if (datas[i].isselect==='是'){
                    dNmarking+=datas[i].fieldName+",";
                    dNmarkingdesc+=datas[i].FieldDoc+",";
                }
                //判断是否是唯一
                if (datas[i].isonly==='是'){
                    obj.isonly=true;
                }else if (datas[i].isonly==='否'){
                    obj.isonly=false;
                }
                tables.push(obj);
            }


            var tabs=JSON.stringify(tables);
            //传入的参数 表名称 表描述 查询字段 查询字段描述 要创建的表字段
            var param={
                dName:field.dName,
                dDesc:field.description,
                dNmarking:dNmarking,
                dNmarkingdesc:dNmarkingdesc,
                tables:tabs
            }
            if (dId!=null){
                param["dId"]=dId;
            }
            /*console.log(param);
            return false;*/
            //传递参数  表名 表描述  表字段
            $.post('/TerpServerController/CreateTerpServerTable',param,function(res){
                if (res.flag){
                    layer.msg('操作成功', {icon: 6});
                    parent.closeAll();
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>
</html>
